package com.cloudfun.campusshare.util;

import cn.hutool.core.util.NumberUtil;
import org.apache.commons.lang3.math.NumberUtils;

import java.math.BigDecimal;

/**
 * Created by Huqin on 2020/4/14
 */
public class AmountUtil {

    public static final int DEFAULT_MULTIPLIER = 100;
    public static final BigDecimal DEFAULT_MULTIPLIER_BIG_DECIMAL = new BigDecimal(DEFAULT_MULTIPLIER);

    private AmountUtil() {
    }

    public static int strAmount2Int(String strAmount) {
        return new BigDecimal(strAmount).multiply(DEFAULT_MULTIPLIER_BIG_DECIMAL).intValue();
    }

    public static int strAmount2Int(String strAmount, int multiplier) {
        return new BigDecimal(strAmount).multiply(new BigDecimal(multiplier)).intValue();
    }

    public static String amountRoundStr(int amount, int scale) {
        BigDecimal res = new BigDecimal(amount).divide(DEFAULT_MULTIPLIER_BIG_DECIMAL);
        return NumberUtil.roundStr(res.toString(), scale);
    }

    public static String amountRoundStr(Integer amount, int scale) {
        if (null == amount) {
            return NumberUtil.roundStr(NumberUtils.INTEGER_ZERO, scale);
        }
        return amountRoundStr(amount.intValue(), scale);
    }

    public static String amountRoundStr(long amount, int scale) {
        BigDecimal res = new BigDecimal(amount).divide(DEFAULT_MULTIPLIER_BIG_DECIMAL);
        return NumberUtil.roundStr(res.toString(), scale);
    }

    public static String amountRoundStr(Long amount, int scale) {
        if (null == amount) {
            return NumberUtil.roundStr(NumberUtils.LONG_ZERO, scale);
        }
        return amountRoundStr(amount.longValue(), scale);
    }

    public static String amountRoundStr(BigDecimal amount, int scale) {
        if (null == amount) {
            amount = BigDecimal.ZERO;
        }
        BigDecimal res = amount.divide(DEFAULT_MULTIPLIER_BIG_DECIMAL);
        return NumberUtil.roundStr(res.toString(), scale);
    }

}
